package Control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;

import Entity.Workshop_BookWorkshopEntity;
import Entity.Workshop_WorkshopEntity;
import Form.Workshop_ViewBooking_Form;
import Form.Workshop_ViewWorkshop_Form;

public class Workshop_ViewBooking_Controller {
	//Object Attributes
	private JDialog viewSingle;
	private Workshop_WorkshopEntity selectedWorkshop = new Workshop_WorkshopEntity();
	private ArrayList<Workshop_WorkshopEntity> workshopList = new ArrayList<Workshop_WorkshopEntity>();
	private ArrayList<Workshop_BookWorkshopEntity> needyBooking = new ArrayList<Workshop_BookWorkshopEntity>();
	private ArrayList<Workshop_BookWorkshopEntity> workshopBooking = new ArrayList<Workshop_BookWorkshopEntity>();

	private Workshop_ViewBooking_Form parser;
	
	public Workshop_ViewBooking_Form getParser() {
		return parser;
	}

	public void setParser(Workshop_ViewBooking_Form parser) {
		this.parser = parser;
	}
	
	public JDialog getViewSingle() {
		return viewSingle;
	}

	public void setViewSingle(JDialog viewSingle) {
		this.viewSingle = viewSingle;
	}

	public Workshop_WorkshopEntity getSelectedWorkshop() {
		return selectedWorkshop;
	}

	public void setSelectedWorkshop(Workshop_WorkshopEntity selectedWorkshop) {
		this.selectedWorkshop = selectedWorkshop;
	}

	public Workshop_ViewBooking_Controller(final Workshop_ViewBooking_Form parser){
		this.setParser(parser);
		
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
		{

			@Override
			protected Void doInBackground() throws Exception {
				if(CommonControls.getUserType().equals("Staff"))
					workshopList = Workshop_WorkshopEntity.retrieveAllWorkshops(null, "ASC", false, false, null, false, null, null);
				
				return null;
			}
			
			protected void done(){
				if(CommonControls.getUserType().equals("Needy")){
					getParser().getLblName().setText(CommonControls.getUser().getName());
					getParser().getLblName().setVisible(true);
					getParser().getLblTitle().setText("Workshop Name");
					generateNeedyBookingList();
				}
				else{
					getParser().getLblTitle().setText("Needy Resident Name");
					getParser().getWorkshopCombo().setVisible(true);
					getParser().getWorkshopCombo().addItem("Select a workshop..");
					for(int i = 0; i < workshopList.size(); i++){
						getParser().getWorkshopCombo().addItem(workshopList.get(i).getWorkshopName());
					}
				}
			}
		};
		worker.execute();
	}
	
	public void generateNeedyBookingList(){
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
		{
			@Override
			protected Void doInBackground() throws Exception {
				needyBooking = Workshop_BookWorkshopEntity.retrieveAllBookingsForResident(CommonControls.getUser().getNric());
				
				return null;
			}
			
			protected void done(){
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
				getParser().getViewPanel().setPreferredSize(new Dimension(420, needyBooking.size() * 30));
				for(int i = 0; i < needyBooking.size(); i++){
					final int tempIndex = i;
					final JPanel workshop = new JPanel();
					final JLabel workshopName = new JLabel(needyBooking.get(i).getWorkshop().getWorkshopName());
					final JLabel bookingStatus = new JLabel(needyBooking.get(i).getBookingStatusCode().getDescription());
					final JLabel bookingDateTime = new JLabel(sdf.format(needyBooking.get(i).getBookingDateTime()));
					
					workshop.setPreferredSize(new Dimension(420, 30));
					workshop.setOpaque(false);
					FlowLayout layout = (FlowLayout)workshop.getLayout();
					layout.setAlignment(FlowLayout.LEFT);
					
					workshop.addMouseListener(new MouseListener() {

						@Override
						public void mouseClicked(MouseEvent arg0) {
							setSelectedWorkshop(needyBooking.get(tempIndex).getWorkshop());
							setViewSingle(new JDialog(parser.getParser()));
							viewSingle.add(new Workshop_ViewWorkshop_Form(parser));
							viewSingle.pack();
							viewSingle.setLocationRelativeTo(parser.getParser());
							viewSingle.setVisible(true);
						}

						@Override
						public void mouseEntered(MouseEvent arg0) {
							workshop.setOpaque(true);
							workshop.setBackground(SystemColor.desktop);
							workshopName.setForeground(Color.WHITE);
							bookingStatus.setForeground(Color.WHITE);
							bookingDateTime.setForeground(Color.WHITE);
						}

						@Override
						public void mouseExited(MouseEvent arg0) {
							workshop.setOpaque(false);
							workshop.setBackground(null);
							workshopName.setForeground(null);
							bookingStatus.setForeground(null);
							bookingDateTime.setForeground(null);
						}

						@Override
						public void mousePressed(MouseEvent arg0) {
						}
						@Override
						public void mouseReleased(MouseEvent arg0) {
						}
					});

					workshopName.setPreferredSize(new Dimension(165, 20));
					workshopName.setHorizontalAlignment(SwingConstants.LEFT);
					workshop.add(workshopName);

					bookingStatus.setPreferredSize(new Dimension(77, 20));
					bookingStatus.setHorizontalAlignment(SwingConstants.CENTER);
					workshop.add(bookingStatus);

					bookingDateTime.setPreferredSize(new Dimension(160, 20));
					bookingDateTime.setHorizontalAlignment(SwingConstants.CENTER);
					workshop.add(bookingDateTime);
					
					getParser().getViewPanel().add(workshop);
				}
				getParser().getViewPanel().repaint();
				getParser().getViewPanel().revalidate();
				parser.getParser().closeProgressBar();
			}
		};
		parser.getParser().getProgressBar();
		worker.execute();
	}
	
	public void generateWorkshopBookingList(){
		getParser().getViewPanel().removeAll();
		getParser().getViewPanel().repaint();
		getParser().getViewPanel().revalidate();
		
		if(getParser().getWorkshopCombo().getSelectedIndex() > 0){
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
			{
				@Override
				protected Void doInBackground() throws Exception {
					int index = getParser().getWorkshopCombo().getSelectedIndex()-1;
					workshopBooking = Workshop_BookWorkshopEntity.retrieveAllBookingsForWorkshop(workshopList.get(index).getWorkshopId());
					
					return null;
				}
				
				protected void done(){
					SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
					getParser().getViewPanel().setPreferredSize(new Dimension(420, workshopBooking.size() * 30));
					for(int i = 0; i < workshopBooking.size(); i++){
						final JPanel workshop = new JPanel();
						JLabel needyName = new JLabel(workshopBooking.get(i).getNeedyResident().getName());
						JLabel bookingDateTime = new JLabel(sdf.format(workshopBooking.get(i).getBookingDateTime()));
						JLabel bookingStatus = new JLabel(workshopBooking.get(i).getBookingStatusCode().getDescription());
						
						workshop.setPreferredSize(new Dimension(420, 30));
						workshop.setOpaque(false);
						FlowLayout layout = (FlowLayout)workshop.getLayout();
						layout.setAlignment(FlowLayout.LEFT);
						
						needyName.setPreferredSize(new Dimension(165, 20));
						needyName.setHorizontalAlignment(SwingConstants.CENTER);
						workshop.add(needyName);
						
						bookingStatus.setPreferredSize(new Dimension(77, 20));
						bookingStatus.setHorizontalAlignment(SwingConstants.CENTER);
						workshop.add(bookingStatus);
						
						bookingDateTime.setPreferredSize(new Dimension(160, 20));
						bookingDateTime.setHorizontalAlignment(SwingConstants.CENTER);
						workshop.add(bookingDateTime);
						
						getParser().getViewPanel().add(workshop);
					}
					getParser().getViewPanel().repaint();
					getParser().getViewPanel().revalidate();
					parser.getParser().closeProgressBar();
				}
			};
			parser.getParser().getProgressBar();
			worker.execute();
		}
	}
}
